﻿using Crgd.UAuthServer.T4.Helpers;
using System;
using System.Collections.Generic;
using System.Text;

namespace Crgd.UAuthServer.T4.Entities
{
    /// <summary>
    /// 表字段结构
    /// </summary>
    public sealed class DbColumn
    {
        /// <summary>
        /// 设置或获取 字段ID
        /// </summary>
        public int ColumnID { get; set; }

        /// <summary>
        /// 设置或获取 表名
        /// </summary>
        public string TableName { get; set; }

        /// <summary>
        /// 设置或获取 是否主键
        /// </summary>
        public bool IsPrimaryKey { get; set; }

        /// <summary>
        /// 设置或获取 字段名称
        /// </summary>
        public string ColumnName { get; set; }

        /// <summary>
        /// 设置或获取 字段类型
        /// </summary>
        public string ColumnType { get; set; }

        /// <summary>
        /// 设置或获取 梅举类型字符串：@@CommonBillStatus@@公共订单状态;待确认,0,UnChecked;已确认,1,Checked;已取消,90,Canceled
        /// </summary>
        public string EnumTypeString { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string CSharpDescription { get; set; }

        /// <summary>
        /// 获取 数据库类型对应的C#类型
        /// </summary>
        public string CSharpType
        {
            get
            {
                string _csharpType = null;
                if (!string.IsNullOrEmpty(EnumTypeString))
                {
                    _csharpType = EnumTypeString.Split(new string[] { @"@@" }, StringSplitOptions.RemoveEmptyEntries)[0];
                }
                else
                {
                    _csharpType = SqlServerDbTypeMap.MapCsharpType(ColumnType);
                }

                if (IsNullable && _csharpType.ToLower() != "string" && _csharpType != "System.String" && _csharpType.ToLower() != "byte[]" && _csharpType.ToLower() != "system.byte[]" && _csharpType != "object")
                {
                    return string.Concat(_csharpType, "?");
                }

                return _csharpType;
            }
        }
        /// <summary>
        /// 获取 公共类型
        /// </summary>
        public Type CommonType
        {
            get
            {
                return SqlServerDbTypeMap.MapCommonType(ColumnType);
            }
        }

        /// <summary>
        /// 设置或获取 字节长度
        /// </summary>
        public int ByteLength { get; set; }

        /// <summary>
        /// 设置或获取 字符长度
        /// </summary>
        public int CharLength { get; set; }

        /// <summary>
        /// 设置或获取 小数位
        /// </summary>
        public int Scale { get; set; }

        /// <summary>
        /// 设置或获取 是否自增列
        /// </summary>
        public bool IsIdentity { get; set; }

        /// <summary>
        /// 设置或获取 是否允许空
        /// </summary>
        public bool IsNullable { get; set; }

        /// <summary>
        /// 设置或获取 默认值
        /// </summary>
        public string DefaultValue { get; set; }

        /// <summary>
        /// 设置或获取 描述
        /// </summary>
        public string Remark { get; set; }
    }
}
